/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.ui;

import com.bytedance.scene.ViewFinder;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.agp.components.Component;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;

import java.io.IOException;

/**
 * @hide
 */
public final class FragmentViewFinder implements ViewFinder {
    private final Fraction mFragment;

    FragmentViewFinder(Fraction fragment) {
        this.mFragment = fragment;
    }

    @Override
    public <T extends Component> T requireViewById(int viewId) {
        T view = (T) mFragment.getComponent().findComponentById(viewId);
        if (view == null) {

            Resource viewIdName = null;
            try {
                viewIdName = mFragment.getResourceManager().getResource(viewId);
                throw new IllegalArgumentException(" " + viewIdName + " view not found");
            } catch (IOException | NotExistException e) {
                throw new IllegalArgumentException(" " + viewId + " view not found");
            }


        }
        return view;
    }
}
